package org.cp.action;

import java.util.Map;

import org.cp.service.user.ITestUserService;
import org.cp.vo.Testuser;

import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;

@SuppressWarnings("serial")
public class TestUserAction extends ActionSupport {

	protected Testuser testUser;

	protected ITestUserService testUserService;

	public ITestUserService getTestUserService() {
		return testUserService;
	}

	public void setTestUserService(ITestUserService testUserService) {
		this.testUserService = testUserService;
	}

	public Testuser getTestUser() {
		return testUser;
	}

	public void setTestUser(Testuser testUser) {
		this.testUser = testUser;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {

		return ERROR;
	}

	public String register() throws Exception {

		testUserService.register(testUser);
		return SUCCESS;
	}

	// public String deleteUser() throws Exception {
	//
	// testUserService.deleteUser(testUser);
	// return SUCCESS;
	// }

	public String changeUserInfo() throws Exception {

		testUserService.changeUserInfo(testUser);
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String login() throws Exception {
		Testuser u = testUserService.login(testUser.getAccounts(), testUser
				.getPassword());
		if (u != null) {
			Map session = ActionContext.getContext().getSession();
			session.put("testUser", u);
			return SUCCESS;
		}

		return ERROR;

	}

	public String logout() throws Exception {

		Map session = ActionContext.getContext().getSession();
		session.remove("testUser");
		return SUCCESS;
	}

}
